#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "winmm.lib")

#include "GUIDirect9.h"

LPDIRECT3D9		  DIRECT9::_pD3D	= NULL;
LPDIRECT3DDEVICE9 DIRECT9::_pDevice = NULL;
LPD3DXSPRITE	  DIRECT9::_pSprite = NULL;

D3DPRESENT_PARAMETERS DIRECT9::_d3dpp;

DIRECT9::DIRECT9() {}

DIRECT9::~DIRECT9() {}

HRESULT DIRECT9::Init(HWND hWnd) 
{
	if(NULL == (_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
		return E_FAIL;

	ZeroMemory(&_d3dpp, sizeof(_d3dpp));

	_d3dpp.Windowed = TRUE;
	_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	_d3dpp.EnableAutoDepthStencil = TRUE;
	_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	
	if(FAILED(_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&_d3dpp, &_pDevice)))
		return E_FAIL;

	if(FAILED(D3DXCreateSprite(_pDevice, &_pSprite)))
		return E_FAIL;
	
	return S_OK;
}

HRESULT DIRECT9::InitSprite(VOID)
{
	if(FAILED(D3DXCreateSprite(_pDevice, &_pSprite)))
		return E_FAIL;
	return S_OK;
}

VOID DIRECT9::Cleanup() 
{
	if(NULL != _pSprite) _pSprite->Release();
	if(NULL != _pDevice) _pDevice->Release();
	if(NULL != _pD3D) _pD3D->Release();
}

VOID DIRECT9::DeviceRelease() 
{
	if(NULL != _pDevice) _pDevice->Release();
}

VOID DIRECT9::SpriteRelease(VOID) 
{
	if(NULL != _pSprite) _pSprite->OnLostDevice();
}
